package com.hc.api.controller;

import com.hc.api.utils.ApiUtil;
import com.hc.comm.base.BaseController;
import com.hc.comm.dto.*;
import com.hc.comm.utils.JsonUtils;
import com.hc.comm.utils.StringUtil;
import com.hc.service.dao.BannersMapper;
import com.hc.service.model.*;
import com.hc.service.service.*;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * Created by pc on 2017/4/24 0024.
 */
@Controller
@Scope("prototype")
@RequestMapping("/" + ApiUtil.ApiVersion.V1 + "/insertdata/")
public class InestData extends BaseController {
    @Resource(name = "bannerTypeService")
    private IBannerTypeService bannerTypeService;

    @Resource(name = "bannersService")
    private IBannersService bannersService;

    @Resource(name = "commentService")
    private ICommentService commentService;

    @Resource(name = "commentExterService")
    private ICommentExterService commentExterService;

    @Resource(name = "commentInfoService")
    private ICommentInfoService commentInfoService;

    @Resource(name = "hospitalService")
    private IHospitalService hospitalService;

    @Resource(name = "hospitalLabelsService")
    private IHospitalLabelsService hospitalLabelsService;

    @Resource(name = "hospitalTypeService")
    private IHospitalTypeService hospitalTypeService;

    @Resource(name = "imageService")
    private IImageService imageService;

    @Resource(name = "labelsAndHospitalService")
    private ILabelsAndHospitalService labelsAndHospitalService;

    @Resource(name = "likeCommentService")
    private ILikeCommentService likeCommentService;

    @Resource(name = "loginInfoService")
    private ILoginInfoService loginInfoService;

    @Resource(name = "managerUserService")
    private IManagerUserService managerUserService;

    @Resource(name = "orderService")
    private IOrderService orderService;

    @Resource(name = "voucherDataService")
    private IVoucherDataService voucherDataService;

    @Resource(name = "voucherTypeService")
    private IVoucherTypeService voucherTypeService;

    @RequestMapping("insertBannerType")
    public void insertBannerTypeData(@ModelAttribute BannerTypeDTO dto) throws Exception {
        if (dto == null || StringUtil.isEmpty(dto.getTypeName())
                || StringUtil.isEmpty(dto.getDescrption())) {
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        BannerTypeModel model = new BannerTypeModel();
        model.setCreateTime((int) (System.currentTimeMillis()/1000));
        model.setDescrption(dto.getDescrption());
        model.setTypeName(dto.getTypeName());
        model.setUpdateTime((int) (System.currentTimeMillis()/1000));
        model = bannerTypeService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }

    @RequestMapping("bannersService")
    public void insertBannersData(@ModelAttribute BannersDTO dto) throws Exception {
        if (dto==null||StringUtil.isEmpty(dto.getBannerContent())||StringUtil.isEmpty(dto.getBannerTitle())||
                StringUtil.isEmpty(dto.getImageUrl())||StringUtil.isEmpty(dto.getLinkUrl())){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        BannersModel model = new BannersModel();
        model.setBannerContent(dto.getBannerContent());
        model.setBannerTitle(dto.getBannerTitle());
        model.setCreateTime((int) (System.currentTimeMillis()/1000));
        model.setImageUrl(dto.getImageUrl());
        model.setLinkUrl(dto.getLinkUrl());
        model.setShowSort(dto.getShowSort());
        model.setState(dto.getState());
        model.setTypeId(dto.getTypeId());
        model.setUpdateTime((int) (System.currentTimeMillis()/1000));
        model = bannersService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }

    @RequestMapping("commentService")
    public void insertCommentData(@ModelAttribute CommentDTO dto)throws Exception{
        if (dto==null||dto.getCommentId()==0||StringUtil.isEmpty(dto.getContent())||
                dto.getReplyId()==0||dto.getUserId()==0||StringUtil.isEmpty(dto.getAttachment())){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
            CommentModel model=new CommentModel();
        model.setAttachment(dto.getAttachment());//附件地址“-”分隔符分割多个附件地址
        model.setCommentId(dto.getCommentId());//评论的id
        model.setCommentState(dto.getCommentState());//评论的状态
        model.setCommentType(dto.getCommentType());//评论对象的类型
        model.setContent(dto.getContent());//评论内容
        model.setCreateTime((int) (System.currentTimeMillis()/1000));
        model.setUpdateTime((int) (System.currentTimeMillis()/1000));
        model.setGoodsId(dto.getGoodsId());//评论物品对应的id
        model.setReplyId(dto.getReplyId());//回复的评论的id
        model.setScore(dto.getScore());//给评论物品打分
        model.setUserDegree(dto.getUserDegree());//用户的身份,1普通用户，2医院用户，3管理员
        model.setUserId(dto.getUserId());//用户id
        model=commentService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }

    @RequestMapping("commentExterService")
    public void insertCommentExterData(@ModelAttribute CommentExterDTO dto)throws Exception{
        if (dto==null||dto.getGoodsType()==null||dto.getGoodsId()==null){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        CommentExterModel model=new CommentExterModel();
        model.setCompreScore(dto.getCompreScore());
        model.setCreateTime((int) (System.currentTimeMillis()/1000));
        model.setUpdateTime((int) (System.currentTimeMillis()/1000));
        model.setGoodsId(dto.getGoodsId());
        model.setGoodsType(dto.getGoodsType());
        model=commentExterService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }

    @RequestMapping("commentInfoService")
    public void insertCommentInfoData(@ModelAttribute CommentInfoDTO dto)throws Exception{
        if (dto==null ||dto.getCommentId()==null||dto.getManagerId()==null){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        CommentInfoModel model=new CommentInfoModel();
        model.setAction(dto.getAction());
        model.setCommentId(dto.getCommentId());
        model.setManagerId(dto.getManagerId());
        model.setNormalUser(dto.getNormalUser());
        model=commentInfoService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }

    @RequestMapping("hospitalService")
    public void insertHospitalData(@ModelAttribute HospitalDTO dto)throws Exception{
        if (dto==null||StringUtil.isEmpty(dto.getHospitalName())||StringUtil.isEmpty(dto.getHospitalAdress())||
               dto.getHospitalDescription()==null||dto.getTypeId()==null){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        HospitalModel model=new HospitalModel();
        model.setCreateTime((int) (System.currentTimeMillis()/1000));
        model.setUpdateTime((int) (System.currentTimeMillis()/1000));
        model.setHospitalAdress(dto.getHospitalAdress());
        model.setHospitalDescription(dto.getHospitalDescription());
        model.setHospitalName(dto.getHospitalName());
        model.setImageUrl(dto.getImageUrl());
        model.setLatitude(dto.getLatitude());
        model.setLongitude(dto.getLongitude());
        model.setTypeId(dto.getTypeId());
        model= hospitalService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }

    @RequestMapping("hospitalLabelsService")
    public void insertHospitalLabelsData(@ModelAttribute HospitalLabelsDTO dto)throws Exception{
        if (dto==null||StringUtil.isEmpty(dto.getLabelName())){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        HospitalLabelsModel model=new HospitalLabelsModel();
        model.setCreateTime((int) (System.currentTimeMillis()/1000));
        model.setUpdateTime((int) (System.currentTimeMillis()/1000));
        model.setLabelName(dto.getLabelName());
        model= hospitalLabelsService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }

    @RequestMapping("hospitalTypeService")
    public void insertHospitalTypeData(@ModelAttribute HospitalTypeDTO dto)throws Exception{
        if (dto==null||StringUtil.isEmpty(dto.getTypeName())||StringUtil.isEmpty(dto.getDescription())){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        HospitalTypeModel model=new HospitalTypeModel();
        model.setCreateTime((int)(System.currentTimeMillis()/1000));
        model.setUpdateTime((int)(System.currentTimeMillis()/1000));
        model.setDescription(dto.getDescription());
        model.setTypeName(dto.getTypeName());
        model=hospitalTypeService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);

    }

    @RequestMapping("imageService")
    public void insertImageData(@ModelAttribute ImageDTO dto)throws Exception{
        if (dto==null||dto.getImageType()==null
                ||StringUtil.isEmpty(dto.getImageUrl())){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        ImageModel model=new ImageModel();
        model.setGoodsId(dto.getGoodsId());
        model.setImageState(dto.getImageState());
        model.setImageType(dto.getImageType());
        model.setImageUrl(dto.getImageUrl());
        model=imageService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }

    @RequestMapping("labelsAndHospitalService")
    public void insertlabelsAndHospitalData(@ModelAttribute LabelsAndHospitalDTO dto)throws Exception{
        if (dto==null||dto.getLabelsId()==null||dto.getHospitalId()==null){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        LabelsAndHospitalModel model=new LabelsAndHospitalModel();
        model.setCreateTime((int)(System.currentTimeMillis()/1000));
        model.setUpdateTime((int)(System.currentTimeMillis()/1000));
        model.setHospitalId(dto.getHospitalId());
        model.setLabelsId(dto.getLabelsId());
        model= labelsAndHospitalService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }

    @RequestMapping("likeCommentService")
    public void insertLikeCommentData(@ModelAttribute LikeCommentDTO dto)throws Exception{
        if (dto==null||dto.getCommentId()==null||dto.getLikeState()==null||dto.getUserId()==null){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        LikeCommentModel model=new LikeCommentModel();
        model.setCommentId(dto.getCommentId());
        model.setLikeState(dto.getLikeState());
        model.setUserId(dto.getUserId());
        model=likeCommentService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }
    @RequestMapping("loginInfoService")
    public void insertLoginInfoData(@ModelAttribute LoginInfoDTO dto)throws Exception{
        if (dto==null||StringUtil.isEmpty(dto.getAddress())||StringUtil.isEmpty(dto.getClient())
                ||StringUtil.isEmpty(dto.getDeviceId())||dto.getUserId()==null){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        LoginInfoModel model=new LoginInfoModel();
        model.setCreateTime(System.currentTimeMillis()/1000);
        model.setUpdateTime(System.currentTimeMillis()/1000);
        model.setAddress(dto.getAddress());
        model.setClient(dto.getClient());
        model.setDeviceId(dto.getDeviceId());
        model.setIp(dto.getIp());
        model.setLatitude(dto.getLatitude());
        model.setLongitude(dto.getLongitude());
        model.setUserId(dto.getUserId());
        model=loginInfoService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }

    @RequestMapping("managerUserService")
    public void insertManagerUserData(@ModelAttribute ManagerUserDTO dto)throws Exception{
        if (dto==null||dto.getHospitalId()==null||dto.getLevel()==null
                ||StringUtil.isEmpty(dto.getPassword())||StringUtil.isEmpty(dto.getUserName())){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        ManagerUserModel model=new ManagerUserModel();
        model.setCreateTime((int)(System.currentTimeMillis()/1000));
        model.setUpdateTime((int)(System.currentTimeMillis()/1000));
        model.setHospitalId(dto.getHospitalId());
        model.setLevel(dto.getLevel());
        model.setPassword(dto.getPassword());
        model.setUserName(dto.getUserName());
        model=managerUserService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }

    @RequestMapping("orderService")
    public void insertOrderData(@ModelAttribute OrderDTO dto)throws Exception{
        if (dto==null||dto.getOrderState()==null||dto.getOrderMoney()==null
                ||StringUtil.isEmpty(dto.getRemarks())||dto.getUserId()==null
                ||dto.getVoucherId()==null){
            JsonUtils.toErrorResult(request, response, "参数不完整！");
            return;
        }
        OrderModel model=new OrderModel();
        model.setCreateTime((int)(System.currentTimeMillis()/1000));
        model.setUpdateTime((int)(System.currentTimeMillis()/1000));
        model.setOrderMoney(dto.getOrderMoney());
        model.setOrderState(dto.getOrderState());
        model.setRemarks(dto.getRemarks());
        model.setUserId(dto.getUserId());
        model.setVoucherId(dto.getVoucherId());
        model=orderService.insert(model);
        JsonUtils.toSuccessResult(request, response, model);
    }

    @RequestMapping("voucherDataService")
    public void insertVoucherData(@ModelAttribute VoucherDataDTO dto)throws Exception{
        VoucherDataModel model=new VoucherDataModel();
        model.setCreateTime((int)(System.currentTimeMillis()/1000));
        model.setUpdateTime((int)(System.currentTimeMillis()/1000));
        model.setEndTime(model.getEndTime());
    }


}
